package com.example.demo.reflect.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author xkx
 * @description
 * @since 2019/3/22 18:15
 */
public class PersonServiceProxy implements InvocationHandler {

  public static final Logger logger = LoggerFactory.getLogger(PersonServiceProxy.class);

  private PersonService personService;

  public PersonServiceProxy(PersonService personService) {
    this.personService = personService;
  }

  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if ("doSomething".equals(method.getName())) {
      logger.info("doSomething调用之前");
      method.invoke(personService, args);
      logger.info("doSomething调用之后");
    } else if ("saySomething".equals(method.getName())) {
      logger.info("saySomething调用之前");
      method.invoke(personService, args);
      logger.info("saySomething调用之后");
    }
    return null;
  }
}
